﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using fanfu.Business;
using fanfu.Model;
using fanfu.Common;
using System.Text;
using System.Data;

public partial class Leaveword : BasePage
{
    public StringBuilder leavewordListHtml = new StringBuilder();

    protected void Page_Load(object sender, EventArgs e)
    {
        IPLocation ipLocation = new QQWryLocator().Query(Request.UserHostAddress);
        this.LeaveIP.Value = ipLocation.IP + ipLocation.Country + ipLocation.Local;
        int page;
        try
        {
            page = Convert.ToInt32(Request["page"].ToString());
            if (page <= 0) //如果输入的页数小于等于0则跳转到第一页
                page = 1;
        }
        catch (Exception ex)
        {
            page = 1;
        }

        leavewordListHtml = CreateLeavewordListHtml(page);
    }

    #region 创建留言列表和分页信息
    /// <summary>
    /// 创建留言列表和分页信息
    /// </summary>
    /// <param name="page">当前的页码,从1开始</param>
    /// <returns></returns>
    public StringBuilder CreateLeavewordListHtml(int page)
    {
        StringBuilder tempHtml = new StringBuilder();

        LeavewordModel leavewordModel = new LeavewordModel();
        int topValue = __PageSize * (page - 1);
        string sqlString = "SELECT TOP " + __PageSize + " * FROM Leaveword WHERE ID NOT IN (SELECT TOP " + topValue + " ID FROM Leaveword WHERE Flag=" + (int)LeavewordFlag.passVerify + " ORDER BY LeaveTime DESC) AND Flag=" + (int)LeavewordFlag.passVerify + " ORDER BY LeaveTime DESC";
        DataTable leaveword = SqlHelper.GetDataTable(sqlString);
        int leavewordCount = new LeavewordRule().GetAllLeavewordInfoByPassVerify().Rows.Count;
        tempHtml.AppendFormat("<h2 class=\"title\">大家对我说：<span>共有留言<strong id=\"topCommentCount\">{0}</strong>条</span></h2>", leavewordCount);
        tempHtml.Append("<ul class=\"content\">");
        for (int i=0; i<leaveword.Rows.Count; i++)
        {
            tempHtml.Append("<li>");
            tempHtml.AppendFormat("<div class=\"nick\">{0}<span>{1}</span><em>#{2}</em></div>", leaveword.Rows[i]["LeaveName"].ToString(), leaveword.Rows[i]["LeaveTime"].ToString(), i + 1+(page-1)*__PageSize);
            tempHtml.AppendFormat("<div class=\"leaveword_title\"><b>标题：</b>{0}</div>", leaveword.Rows[i]["LeaveCaption"].ToString());
            tempHtml.AppendFormat("<p><b>内容：</b>{0}</p>", leaveword.Rows[i]["LeaveContent"].ToString());
            //判断是否已经回复留言
            if (leaveword.Rows[i]["AnswerFlag"].ToString() == ((int)LeavewordAnswerFlag.yesAnswer).ToString())
            {
                tempHtml.AppendFormat("<div class=\"reply\"><div class=\"nick\">{0}<span>{1}</span></div><p>{2}</p></div>",
                    leaveword.Rows[i]["AnswerName"].ToString(), leaveword.Rows[i]["AnswerTime"].ToString(), leaveword.Rows[i]["AnswerContent"].ToString());
            }
            tempHtml.Append("</li>");
        }
        tempHtml.Append("</ul>");
        tempHtml.Append("<div id=\"paging\">");
        tempHtml.Append(CreatePaging(leavewordCount, page, "Leaveword.aspx?current=3"));
        tempHtml.Append("</div>");
        return tempHtml;
    }
    #endregion
}